package com.example.smartstart.controller;

import com.example.smartstart.entity.Admin;
import com.example.smartstart.model.Result;
import com.example.smartstart.service.IAdminService;
import com.example.smartstart.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dlx
 * @since 2024-07-22
 */
@RestController
@RequestMapping("admin/")
public class AdminController {

    @Autowired
    private IAdminService iAdminService;

    @PostMapping("register")
    public Result register(@RequestBody Admin admin) {
        Admin admin1 = iAdminService.findByUserName(admin.getUsername());
        if (admin1 != null) {
            return Result.error("用户名被占用");
        } else {
            iAdminService.add(admin.getUsername(), admin.getPassword());
            return Result.success();
        }

    }

    @PostMapping("login")
    public Result login(@RequestBody Admin admin) {
        Admin admin1 = iAdminService.findByUserName(admin.getUsername());
        if (admin1 == null) {
            return Result.error("用户名不存在");
        }
        if (Md5Util.getMD5String(admin.getPassword()).equals(admin1.getPassword())) {
            return Result.success();
        }
        return Result.error("用户名或者密码错误");
    }

}
